package cn.xi.service.checkItem;

import cn.xi.checkItem.OrdersettingService;
import cn.xi.dao.OrdersettingDao;
import cn.xi.pojo.OrderSetting;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = OrdersettingService.class)
@Transactional
public class OrdersettingServiceImpl implements OrdersettingService {
    @Autowired
    private OrdersettingDao ordersettingDao;

    @Override
    public void add(List<OrderSetting> list) {
        if (list != null) {
            for (OrderSetting orderSetting : list) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd");
                String date = simpleDateFormat.format(orderSetting.getOrderDate());
                int id = ordersettingDao.selectid(date);
                if (id > 0) {
                    ordersettingDao.update(date,orderSetting.getNumber());
                } else {
                    ordersettingDao.add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> select(String date) {
        List<OrderSetting> select = ordersettingDao.select(date + "-01", date + "-31");

        List<Map> list = new ArrayList<>();
        for (OrderSetting orderSetting : select) {
            Map map = new HashMap();
            Date orderDate = orderSetting.getOrderDate();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd");
            String format = simpleDateFormat.format(orderDate);
            map.put("date", format);
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());
            list.add(map);
        }
        return list;
    }

    @Override
    public void update(Date date, String number) {
         int num = Integer.parseInt(number);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd");
        String date1 = simpleDateFormat.format(date);
        int selectid = ordersettingDao.selectid(date1);
        OrderSetting orderSetting =new OrderSetting(date,num);
        if (selectid > 0){
            ordersettingDao.update(date1,num);
        }else {
            ordersettingDao.add(orderSetting);
        }
    }
}
